src/app/dashboard/applications/[id]/edit/page.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
"use client"; import { useState, useEffect, use } from "react"; import { useRouter } from "next/navigation"; import Button from "@/components/shared/Button"; import Input from "@/components/shared/Input"; export default function EditApplicationPage({ params }) { const resolvedParams = use(params); const router = useRouter(); const [application, setApplication] = useState(null); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [newKey, setNewKey] = useState(null); useEffect(() => { fetchApplication(); }, []); async function fetchApplication() { try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/GetApplication", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to fetch application"); } setApplication(data.application); setName(data.application.name); setDescription(data.application.description); } catch (error) { setError(error.message || "Failed to load application"); console.error("Error:", error); } finally { setIsLoading(false); } } async function handleSubmit(e) { e.preventDefault(); setIsSubmitting(true); setError(""); try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/UpdateApplication", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, name, description, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to update application"); } router.push(`/dashboard/applications/${resolvedParams.id}`); } catch (error) { setError(error.message || "Failed to update application"); console.error("Error:", error); } finally { setIsSubmitting(false); } } async function handleRegenerateKey() { if ( !confirm( "Are you sure you want to regenerate the API key? The old key will stop working immediately.", ) ) { return; } try { const response = await fetch( "http://localhost:8080/twirp/applications.ApplicationsService/RegenerateKey", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), application_id: resolvedParams.id, }), }, ); const data = await response.json(); if (data.code || data.msg) { throw new Error(data.msg || "Failed to regenerate key"); } setNewKey(data.key); } catch (error) { setError(error.message || "Failed to regenerate key"); console.error("Error:", error); } } if (isLoading) return <div>Loading...</div>; if (!application) return <div>Application not found</div>; return ( <div className="space-y-6"> {newKey && ( <div className="bg-white shadow rounded-lg p-6 mb-6"> <h3 className="text-lg font-medium text-gray-900 mb-4"> New API Key Generated </h3> <p className="text-sm text-gray-500 mb-4"> Please copy your new API key now. You won't be able to see it again! </p> <div className="bg-gray-50 p-4 rounded-md mb-4"> <code className="text-sm break-all">{newKey}</code> </div> <Button variant="secondary" onClick={() => { navigator.clipboard.writeText(newKey); }} > Copy to Clipboard </Button> </div> )} <div className="bg-white shadow rounded-lg p-6"> <form onSubmit={handleSubmit} className="space-y-6"> <div> <h2 className="text-lg font-medium text-gray-900"> Edit Application </h2> </div> {error && ( <div className="p-3 bg-red-100 border border-red-400 text-red-700 rounded"> {error} </div> )} <div className="space-y-4"> <Input label="Application Name" value={name} onChange={(e) => setName(e.target.value)} required /> <div> <label className="block text-sm font-medium text-gray-700"> Description </label> <textarea value={description} onChange={(e) => setDescription(e.target.value)} className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" rows={3} /> </div> </div> <div className="border-t border-gray-200 pt-6"> <div className="flex justify-between items-center"> <div> <h3 className="text-sm font-medium text-gray-900">API Key</h3> <p className="mt-1 text-sm text-gray-500"> Generate a new API key if the current one has been compromised. </p> </div> <Button type="button" variant="secondary" onClick={handleRegenerateKey} > Generate New Key </Button> </div> </div> <div className="flex justify-end space-x-4"> <Button variant="secondary" type="button" onClick={() => router.push(`/dashboard/applications/${resolvedParams.id}`) } > Cancel </Button> <Button type="submit" disabled={isSubmitting}> {isSubmitting ? "Saving..." : "Save Changes"} </Button> </div> </form> </div> </div> ); } |